package com.elektronikforumet.camalert;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ChangedCamerasParser extends DefaultHandler {

	/*
	  	<SpeedCamera>
  			<ID>1069</ID> 
			<Longitude>11.36122</Longitude> 
			<Latitude>58.70439</Latitude> 
			<Speed>90</Speed> 
			<Direction>South</Direction> 
			<RoadNumber>E6</RoadNumber> 
			<CameraNumber>5</CameraNumber> 
			<Deleted>false</Deleted> 
		</SpeedCamera>
	 */
	
	private static final String DIRECTION_NORTH = "North";
	private static final String DIRECTION_EAST = "East";
	private static final String DIRECTION_SOUTH = "South";
	private static final String DIRECTION_WEST = "West";
	
	private boolean mInSpeedCamera = false;
	private boolean mInID = false;
	private boolean mInLongitude = false;
	private boolean mInLatitude = false;
	private boolean mInSpeed = false;
	private boolean mInDirection = false;
	private boolean mInRoadNumber = false;
	private boolean mInCameraNumber = false;
	private boolean mInDeleted = false;
	private SpeedCamera mTempCam = null;
	private ArrayList<SpeedCamera> mCams = new ArrayList<SpeedCamera>();
	
	public ArrayList<SpeedCamera> getCameras() {
		return mCams;
	}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if(mInSpeedCamera) {
			if(localName.equals("ID")) {
				mInID = true;
			} else if(localName.equals("Longitude")) {
				mInLongitude = true;
			} else if(localName.equals("Latitude")) {
				mInLatitude = true;
			} else if(localName.equals("Speed")) {
				mInSpeed = true;
			} else if(localName.equals("Direction")) {
				mInDirection = true;
			} else if(localName.equals("RoadNumber")) {
				mInRoadNumber = true;
			} else if(localName.equals("CameraNumber")) {
				mInCameraNumber = true;
			} else if(localName.equals("Deleted")) {
				mInDeleted = true;
			}
		} else {
			if(localName.equals("SpeedCamera")) {
				mInSpeedCamera = true;
				mTempCam = new SpeedCamera();
			}
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(mInSpeedCamera) {
			if(localName.equals("ID")) {
				mInID = false;
			} else if(localName.equals("Longitude")) {
				mInLongitude = false;
			} else if(localName.equals("Latitude")) {
				mInLatitude = false;
			} else if(localName.equals("Speed")) {
				mInSpeed = false;
			} else if(localName.equals("Direction")) {
				mInDirection = false;
			} else if(localName.equals("RoadNumber")) {
				mInRoadNumber = false;
			} else if(localName.equals("CameraNumber")) {
				mInCameraNumber = false;
			} else if(localName.equals("Deleted")) {
				mInDeleted = false;
			}else if(localName.equals("SpeedCamera")) {
				mInSpeedCamera = false;
				mCams.add(mTempCam);
			}
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(mInID) {
			mTempCam.setId(Integer.parseInt(new String(ch, start, length)));
		} else if(mInLongitude) {
			mTempCam.setLongitude(Float.parseFloat(new String(ch, start, length)));
		} else if(mInLatitude) {
			mTempCam.setLatitude(Float.parseFloat(new String(ch, start, length)));
		} else if(mInSpeed) {
			mTempCam.setSpeed(Integer.parseInt(new String(ch, start, length)));
		} else if(mInDirection) {
			String dir = new String(ch, start, length);
			if(dir.equals(DIRECTION_NORTH)) {
				mTempCam.setDirection(SpeedCamera.DIRECTION_NORTH);
			} else if(dir.equals(DIRECTION_EAST)) {
				mTempCam.setDirection(SpeedCamera.DIRECTION_EAST);
			} else if(dir.equals(DIRECTION_SOUTH)) {
				mTempCam.setDirection(SpeedCamera.DIRECTION_SOUTH);
			} else if(dir.equals(DIRECTION_WEST)) {
				mTempCam.setDirection(SpeedCamera.DIRECTION_WEST);
			}
		} else if(mInRoadNumber) {
			mTempCam.setRoadNumber(new String(ch, start, length));
		} else if(mInCameraNumber) {
			mTempCam.setCameraNumber(Integer.parseInt(new String(ch, start, length)));
		} else if(mInDeleted) {
			mTempCam.setDeleted(Boolean.parseBoolean(new String(ch, start, length)));
		}
	}
}
